#coding=utf-8
import client,util,json

''' this file is to create a datasource for sql check '''

metrics = client.getMetrics()

def constructds():
	ds = {}
	ds['name'] = 'seyren_sql'
	ds['schema'] = 'array'
	ds['schema_type'] = 'json'
	ds['type']= 'graphite'
	ds['hostname'] = '9.186.107.32'
	ds['port'] = 80
	ds['targets'] = ','.join(metrics)
	ds['schema'] = []
	for t in metrics:
		sch = {}
		sch['name'] = t.encode('utf-8').replace('.','').replace('-','')
		sch['datatype'] = 'double'
		ds['schema'].append(sch)
	return ds

def post():
	tennantid = 'dada02c017047c0cfdfafc72'
	#url = 'http://9.186.89.218:8080/cpsrestful1/DataSources?tenantid='+ tennantid + '&source=rest'
	url = 'http://9.181.61.102:8080/cpsrestful1/DataSources?tenantid='+ tennantid + '&source=rest'
	ds = constructds()
	data = json.dumps(ds)
	header = {}
	header['Content-Type']='application/json;charset=UTF-8'	
	util.post(url,data,header)

if __name__ == '__main__':
	post()
